/**
 * 采购订单
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">采购管理</el-breadcrumb-item>
      <el-breadcrumb-item>采购订单</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 头部筛选 -->
    <!-- <span class="el-input">
      单据编号：<el-input
        v-model="screen.number"
        placeholder="请输入单据编号"
        style="width: 220px; margin-top: 20px; margin-right: 30px"
      ></el-input>
      商品信息:
      <el-input
        v-model="screen.message"
        placeholder="请输入条码、名称、规格、型号"
        style="width: 230px; margin-top: 20px; margin-right: 30px"
      ></el-input>
      单据日期：<el-date-picker
        v-model="screen.date"
        type="date"
        placeholder="请选择时间"
      ></el-date-picker>
      <el-button type="primary" style="margin-left: 20px">查询</el-button>
      <el-button style="margin-left: 5px">重置</el-button>
    </span> -->

    <!-- 搜索筛选 -->
    <el-form :inline="true" class="user-search">
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd()"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column
        sortable
        align="center"
        prop="checkTime"
        label="审核日期"
      >
      </el-table-column>
      <el-table-column sortable align="center" prop="id" label="单据编号">
      </el-table-column>
      <el-table-column sortable align="center" prop="auditor" label="审核人">
      </el-table-column>
      <el-table-column
        sortable·
        align="center"
        prop="createTime"
        label="单据日期"
      >
      </el-table-column>
      <el-table-column sortable align="center" prop="creater" label="操作员">
      </el-table-column>
      <el-table-column sortable align="center" prop="totals" label="金额统计">
      </el-table-column>
      <el-table-column sortable align="center" prop="state" label="状态">
      </el-table-column>
      <el-table-column sortable align="center" prop="remark" label="备注">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="120">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleUpdate(scope.$index, scope.row)"
            >查看详情</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="state(scope.$index, scope.row)"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <!-- <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total, sizes,prev, pager, next,jumper"
      :current-page.sync="list.pageNum"
      :page-size="list.pageSize"
      :total="total"
    >
    </el-pagination> -->
    <!-- 添加界面 -->
    <el-dialog
      title="添加"
      :visible.sync="editFormVisible"
      width="30%"
      @click="closeDialog"
    >
      <el-form label-width="120px" :model="editForm" ref="editForm">
        <el-form-item label="供货商选择：">
          <el-select
            v-model="editForm.ware"
            placeholder="请选择供货商"
            @change="timechange($event)"
          >
            <el-option
              v-for="item in brandOptions"
              :key="item.label"
              :label="item.label"
              :value="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品信息：">
          <el-select
            v-model="editForm.goodsname"
            placeholder="请选择商品"
            @change="priceChange($event)"
          >
            <el-option
              v-for="item in productoptins"
              :key="item.id"
              :label="item.label"
              :value="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品单价：" prop="name">
          <el-input
            size="small"
            v-model="editForm.price"
            auto-complete="off"
            placeholder="请输入单价"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品数量：" prop="name">
          <el-input
            size="small"
            v-model="editForm.quantity"
            auto-complete="off"
            placeholder="请输入数量"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="handleDialogConfirm()"
          >保存</el-button
        >
      </div>
    </el-dialog>
    <!-- 查看详情 -->
    <el-dialog
      title="查看详情"
      :visible.sync="editFormVisible2"
      width="30%"
      @click="closeDialog2"
    >
      <el-table
        size="small"
        :data="list"
        highlight-current-row
        v-loading="loading"
        border
        element-loading-text="拼命加载中"
        style="width: 100%"
      >
        <el-table-column
          sortable
          align="center"
          prop="checkTime"
          label="审核日期"
        >
        </el-table-column>
        <el-table-column sortable align="center" prop="id" label="单据编号">
        </el-table-column>
        <el-table-column sortable align="center" prop="auditor" label="审核人">
        </el-table-column>
        <el-table-column
          sortable·
          align="center"
          prop="createTime"
          label="单据日期"
        >
        </el-table-column>
        <el-table-column sortable align="center" prop="creater" label="操作员">
        </el-table-column>
        <el-table-column sortable align="center" prop="totals" label="金额统计">
        </el-table-column>
        <el-table-column sortable align="center" prop="state" label="状态">
        </el-table-column>
        <el-table-column sortable align="center" prop="remark" label="备注">
        </el-table-column>
        <el-table-column
          sortable
          align="center"
          prop="goodsname"
          label="商品名"
        >
        </el-table-column>
        <el-table-column
          sortable
          align="center"
          prop="quantity"
          label="商品数量"
        >
        </el-table-column>
        <el-table-column sortable align="center" prop="ware" label="仓库">
        </el-table-column>
        <el-table-column
          sortable
          align="center"
          prop="money"
          label="单个商品总价"
        >
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog2">取消</el-button>
      </div>
    </el-dialog>
    <!-- 审核 -->
    <el-dialog
      title="添加"
      :visible.sync="editFormVisible3"
      width="30%"
      @click="closeDialog3"
    >
      <el-form label-width="120px" :model="states" ref="editForm">
        <el-form-item label="备注：" prop="remark">
          <el-input
            size="small"
            v-model="states.remark"
            auto-complete="off"
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
        <el-form-item label="审核：">
          <el-select
            v-model="states.state"
            placeholder="请选择审核状态"
          >
            <el-option
              v-for="item in stateOptions"
              :key="item.label"
              :label="item.label"
              :value="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog3">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="handleState()"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getSelect,
  add,
  deletes,
  updateCheck,
  getSelectAll,
} from "../../api/order";

const defaultEditForm = {
  id: null,
  name: null,
  coverPhoto: null,
  examplesPictures: null,
  createTime: null,
  updateTime: null,
  overviewc: " ",
};
// const defaultListQuery = {
//   // orderByField: "create_time",
//   productType: "",
//   goodsname: "",
// };
import { getShop } from "../../api/product";
export default {
  data() {
    return {
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      editFormVisible2: false,
      editFormVisible3: false,
      // isEdit: "添加",
      total: "",
      editForm: {
        // ware: "",
        goodsname: "",
        quantity: null,
        money: null,
        price: null,
      },
      // list: Object.assign({}, defaultListQuery),
      listData: [], //用户数据
      list: [], // 查看详情
      id: null,
      // 审核对象
      states: {
        state: null,
        remark: null,
      },
      brandOptions: [
        {
          value: 0,
          label: "腾讯",
        },
        {
          value: 1,
          label: "阿里巴巴",
        },
        {
          value: 1,
          label: "艾弗森",
        },
      ],
      productoptins: [],
      stateOptions: [
        {
          value: 0,
          label: "审核通过",
        },
        {
          value: 1,
          label: "审核未通过",
        }
      ],
    };
  },
  components: {},

  /**
   * 创建完毕
   */
  created() {
    this.getdata();
  },
  beforeDestroy() {
    // 销毁组件前销毁编辑器
  },
  methods: {
    // 获取列表
    getdata() {
      this.loading = true;
      getSelectAll()
        .then((res) => {
          this.loading = false;
          if (res.code != 200) {
            this.$message({
              type: "info",
              message: res.message,
            });
          } else {
            this.$message({
              type: "success",
              message: res.message,
            });
            this.listData = res.result;
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error("菜单加载失败，请稍后再试！");
        });
    },
    // 添加
    handleAdd() {
      this.editFormVisible = true;
      this.isEdit = false;
    },
    // 供应商名称改变事件重新赋值
    timechange(e) {
      for (let i in this.brandOptions) {
        this.brandOptions.push({
          value: this.brandOptions[i].id,
          label: this.brandOptions[i].itemname,
        });
      }
      let data = {
        vendorname: this.editForm.ware,
      };
      getShop(data)
        .then((res) => {
          this.loading = false;
          if (res.code != 200) {
            this.$message({
              type: "info",
              message: res.message,
            });
          } else {
            this.$message({
              type: "success",
              message: res.message,
            });
            for (let i in res.result) {
              this.productoptins.push({
                value: res.result[i].id,
                label: res.result[i].itemname,
              });
            }
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error("菜单加载失败，请稍后再试！");
        });
    },
    // 单价
    priceChange(e) {
      let data = {
        vendorname: this.editForm.ware,
        itemname: this.editForm.goodsname,
      };
      getShop(data)
        .then((res) => {
          this.loading = false;
          if (res.code != 200) {
            this.$message({
              type: "info",
              message: res.message,
            });
          } else {
            this.$message({
              type: "success",
              message: res.message,
            });
            res.result.map((item) => {
              this.editForm.price = item.price;
            });
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error("菜单加载失败，请稍后再试！");
        });
    },
    //显示查看详情
    handleUpdate(index, row) {
      let data = {
        id: row.id,
      };
      this.editFormVisible2 = true;
      getSelect(data).then((res) => {
        console.log(res);
        this.list = res.result;
      });
    },

    // 编辑、增加页面保存方法
    handleDialogConfirm() {
      this.$confirm("是否要确认?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let data = {
          ware: this.editForm.ware,
          goodsname: this.editForm.goodsname,
          quantity: this.editForm.quantity,
          money: this.editForm.quantity * this.editForm.price,
        };
        add(data).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: "添加成功！",
              type: "success",
            });
          } else {
            this.$message({
              message: res.msg,
              type: "info",
            });
          }
          this.editFormVisible = false;
          this.loading = false;
          this.getdata();
        });
      });
    },
    // 审核
    state(index, row) {
      this.editFormVisible3 = true;
      console.log(row.id);
      this.id = row.id;
    },
    // 审核保存
    handleState() {
      let id = {
        id: this.id,
        state: this.states.state,
        remark: this.states.remark,
      };
      console.log(id);
      updateCheck(id).then((res) => {
        if (res.code == 200) {
            this.$message({
              message: "审核成功！",
              type: "success",
            });
            this.getdata();
          } else {
            this.$message({
              message: res.msg,
              type: "info",
            });
            this.getdata();
          }
          this.editFormVisible3 = false;
      });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    },
    closeDialog2() {
      this.editFormVisible2 = false;
    },
    closeDialog3() {
      this.editFormVisible3 = false;
    },
  },
};
</script>
<style >
.el-dialog {
  width: 60% !important;
}
.el-input--small .el-input__inner {
  width: 70%;
}
.bth {
  float: right;
  margin-top: -30px;
  margin-left: -30px;
}
</style>
<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>